// Author: Domien Nowicki
#ifndef CELLWIDGET_H
#define CELLWIDGET_H

#include "Cell.h"
#include "CellSideWidget.h"
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QCursor>

class CellWidget: public QObject, public QGraphicsItem, public Cell
{
	Q_OBJECT 
public:
    CellWidget(QGraphicsScene *scene, unsigned int mapX, unsigned int mapY);
	CellWidget(QGraphicsScene *scene, const Cell& rhs);
	virtual ~CellWidget();

	virtual void SetWall();
	virtual void SetEmpty();
	virtual void SetTexture(Cell::Side side, unsigned int tindex, const Pixel& userColor);
	virtual void SetMapPos(unsigned int mapX, unsigned int mapY);

protected:
	
	QRectF boundingRect() const;
	QPainterPath shape() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
	void hoverEnterEvent(QGraphicsSceneHoverEvent *e);
	void hoverLeaveEvent(QGraphicsSceneHoverEvent *e);
	void mousePressEvent(QGraphicsSceneMouseEvent *e);

signals:
	void clicked(CellWidget*);
	void clickedSide(CellWidget*, Cell::Side side);

private slots:
	void slotClickedSide(CellSideWidget* side);

private:
	void UpdateSides();
	void Clear();
	void RemoveSide(unsigned int sindex);
	void AddSide(unsigned int sindex);
	void SetupWidget(QGraphicsScene *scene);

	bool highlight;

	CellSideWidget *sides[Cell::SIDECOUNT];

	QRectF bounds;
	QPainterPath path;

	static const unsigned int ITEMSIZE = 64;
	static const unsigned int ITEMZ = 0;
};

#endif // CELLWIDGET_H
